//
//  LGRecoveryDetailHeadView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/7.
//

#import "LGSportsHistoryDetailHeadView.h"

#import "LGRecoveryDetailContent.h"
@interface LGSportsHistoryDetailHeadView()
/**   */
@property (nonatomic , strong)UILabel *doctorLabel;
/**   */
@property (nonatomic , strong)UILabel *dateLabel;
/**   */
@property (nonatomic , strong)UIImageView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *timeTitle;
/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)UIButton *rateTitle;
/**   */
@property (nonatomic , strong)UILabel *rateLabel;
/**   */
@property (nonatomic , strong)UIView *contentView;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UIButton *detailBtn;
/**   */
//@property (nonatomic , strong)UILabel *tipLabel;
/**   */
@property (nonatomic , strong)UILabel *trainTitle;

@end

@implementation LGSportsHistoryDetailHeadView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *dateStr = [LGNSString(dataDic[@"create_time"]) substringToIndex:10];
    self.doctorLabel.text = [NSString stringWithFormat:@"%@医生发放",dataDic[@"doctor_name"]];
    self.dateLabel.text = dateStr;
    self.titleLabel.text = LGNSString(dataDic[@"title"]);
    self.timeLabel.text = [NSString stringWithFormat:@"%@分钟",dataDic[@"duration"]];
    self.rateLabel.text = LGNSString(dataDic[@"frequency"]);
//    self.tipLabel.text = [NSString stringWithFormat:@"%@ %@医生发放",dateStr,dataDic[@"doctor_name"]];
    NSString *content = LGNSString(dataDic[@"description"]);
    self.contentLabel.text = [self contentString:content];
    self.detailBtn.hidden = [[self contentString:content] isEqualToString:content]?YES:NO;
    [self.contentLabel lineSpacing:7];
}

//查看详情
-(void)checkContentDetailAction{
    LGRecoveryDetailContent *contentView = [[LGRecoveryDetailContent alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    contentView.title = LGNSString(self.dataDic[@"title"]);
    contentView.content = LGNSString(self.dataDic[@"description"]);
    [[UIApplication sharedApplication].keyWindow addSubview:contentView];
}

-(NSString *)contentString:(NSString *)content{
    CGFloat height = [content textHeightWithFont:LGFont(15) maxWidth:Screen_W-viewPix(72) lineSpacing:7];
    CGFloat labelHeight = viewPix(48);
    if(height>labelHeight){
        for(NSUInteger i =1; i < content.length; i++){
            NSString *subString = [content substringToIndex:i];
            if ([subString textHeightWithFont:LGFont(15) maxWidth:Screen_W-viewPix(72) lineSpacing:7] > labelHeight){
                return [NSString stringWithFormat:@"%@...",[subString substringToIndex:i-5]];
            }
        }
    }
    return content;
}
 


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.doctorLabel];
    [self addSubview:self.dateLabel];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.timeTitle];
    [self.baseView addSubview:self.timeLabel];
    [self.baseView addSubview:self.rateTitle];
    [self.baseView addSubview:self.rateLabel];
    [self.baseView addSubview:self.contentView];
//    [self.baseView addSubview:self.tipLabel];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.detailBtn];
    [self addSubview:self.trainTitle];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(12));
        make.left.equalTo(self.contentView).offset(viewPix(8));
        make.right.equalTo(self.contentView).offset(-viewPix(8));
    }];
}

-(UILabel *)doctorLabel{
    if(!_doctorLabel){
        _doctorLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _doctorLabel.frame = CGRectMake(viewPix(17), viewPix(16), Screen_W-viewPix(135), viewPix(22));
    }
    return _doctorLabel;
}

-(UILabel *)dateLabel{
    if(!_dateLabel){
        _dateLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(15) textAlignment:NSTextAlignmentRight lines:1];
        _dateLabel.frame = CGRectMake(Screen_W-viewPix(116), viewPix(16), viewPix(100), viewPix(22));
    }
    return _dateLabel;
}


-(UIImageView *)baseView{
    if(!_baseView){
        _baseView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"运动方案-历史方案背景"]];
        _baseView.frame = CGRectMake(viewPix(16), viewPix(54), Screen_W-viewPix(32), viewPix(177));
        _baseView.userInteractionEnabled = YES;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(60), viewPix(22));
    }
    return _titleLabel;
}

-(UIButton *)timeTitle{
    if(!_timeTitle){
        _timeTitle = [UIButton buttonWithTitle:@"时长" titleFont:LGFont(15) textColor:@"#FFFFFF" imageName:@"运动方案-时长"];
        _timeTitle.frame = CGRectMake(viewPix(16), kMaxY(self.titleLabel.frame)+viewPix(12), viewPix(60), viewPix(21));
        _timeTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _timeTitle.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
        _timeTitle.userInteractionEnabled = NO;
    }
    return _timeTitle;
}

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _timeLabel.frame = CGRectMake(viewPix(16), kMaxY(self.timeTitle.frame)+viewPix(2), viewPix(90), viewPix(21));
    }
    return _timeLabel;
}

-(UIButton *)rateTitle{
    if(!_rateTitle){
        _rateTitle = [UIButton buttonWithTitle:@"频率" titleFont:LGFont(15) textColor:@"#FFFFFF" imageName:@"运动方案-频率"];
        _rateTitle.frame = CGRectMake(viewPix(15)+(Screen_W-viewPix(32))/2.0, kMinY(self.timeTitle.frame), viewPix(60), viewPix(21));
        _rateTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _rateTitle.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
        _rateTitle.userInteractionEnabled = NO;
    }
    return _rateTitle;
}

-(UILabel *)rateLabel{
    if(!_rateLabel){
        _rateLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _rateLabel.frame = CGRectMake(kMinX(self.rateTitle.frame), kMinY(self.timeLabel.frame), viewPix(160), viewPix(21));
    }
    return _rateLabel;
}

-(UIView *)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(12), viewPix(100), Screen_W-viewPix(56), viewPix(64))];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.cornerRidus = 5;
    }
    return _contentView;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:2];
    }
    return _contentLabel;
}

//-(UILabel *)tipLabel{
//    if(!_tipLabel){
//        _tipLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
//        _tipLabel.frame = CGRectMake(viewPix(16), kMaxY(self.contentView.frame)+viewPix(5), Screen_W-viewPix(64), viewPix(20));
//    }
//    return _tipLabel;
//}

-(UIButton *)detailBtn{
    if(!_detailBtn){
        _detailBtn = [UIButton buttonWithTitle:@"详情" titleFont:LGFontWeight(15, UIFontWeightMedium) textColor:@"#333333" imageName:@"blackTip16"];
        _detailBtn.frame = CGRectMake(self.contentView.size.width-viewPix(64), viewPix(34), viewPix(58), viewPix(21));
        _detailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _detailBtn.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        [_detailBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
        [_detailBtn addTarget:self action:@selector(checkContentDetailAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _detailBtn;
}

-(UILabel *)trainTitle{
    if(!_trainTitle){
        _trainTitle = [UILabel lableWithText:@"训练目录" colorString:@"#000000" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _trainTitle.frame = CGRectMake(viewPix(16), viewPix(250), viewPix(120), 22);
    }
    return _trainTitle;
}

@end
